package com.lcc.generate;

import com.lcc.generate.util.FileUtils;
import com.lcc.generate.util.JsonUtil;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @author lcc
 * @date 2025/1/13 10:55
 */
public class Config {

    private static Config c;

    /** 数据库类型 */
    public String dbName = "mysys";
    public String model = "";
    /** 数据库名称,postgresql,mysql */
    public String dbType = "postgresql";
    public String dbDriver = "mysys";
    public String dbUrl = "mysys";
    public String dbUsername = "mysys";
    public String dbPassword = "mysys";

    /** 数据库模式 */
    public String schemaPattern = "public";
    /** 作者名称 */
    public String author = "lcc";
    /** 项目根绝对路径 */
    public String rootPath;
    public String rootImplPath;
    public String rootControllerPath;
    public String rootMapperPath;
    /** 包名 */
    public String packageName;
    /** vue页面根路径 */
    public String vuePath = "D:/myproject/idea/my/mysys-ui/src/pages/system";
    public String jspPath = "D:\\project\\idea\\zb\\mskc-js-web\\mskc-web\\src\\main\\webapp\\WEB-INF\\jsp\\base";

    /** 用lombok注解生成get，set */
    public boolean ifLombok;
    /** 字段转驼峰 */
    public boolean ifAutoToHump;
    /** 生成名称不包含表前缀 */
    public boolean ifNoPrefix;
    /** 字段生成label注解 */
    public boolean ifFieldLabel;
    
    /** 是否生成ExcelClass */
    public boolean generateExcelClass;
    /** 是否生成表定义类 */
    public boolean generateDefClass;
    /** 是否生成controller */
    public boolean generateController;
    /** 是否生成service */
    public boolean generateService;

    /** 覆盖BaseClass */
    public boolean coverBaseClass;
    /** 覆盖DV对象 */
    public boolean coverDVFile;
    /** 覆盖Excel对象 */
    public boolean coverExcelClass;
    /** 覆盖Mapper.xml */
    public boolean coverMainMapperXml;
    /** 覆盖Mapper对象 */
    public boolean coverMainMapperClass;
    /** 覆盖Service接口 */
    public boolean coverMainServiceIntf;
    /** 覆盖Service实现 */
    public boolean coverMainServiceImpl;
    /** 覆盖controller */
    public boolean coverMainController;
    /** 覆盖index.vue页面 */
    public boolean coverVueIndex;
    /** 覆盖add.vue页面 */
    public boolean coverVueAdd;
    /** 覆盖update.vue页面 */
    public boolean coverVueUpdate;
    public boolean coverVueInfo;
    public boolean coverMainTs;


    public TableConfig[] generateConfigs;
    public boolean generateXmlAnnotation;

    public void setRootPath(String rootPath) {
        this.rootPath = rootPath;
        this.rootImplPath = rootPath;
        this.rootMapperPath = rootPath;
        this.rootControllerPath = rootPath;
    }

    public void setGenerateConfigs(Object[] generateConfigs) {
        if(generateConfigs.length > 0 && generateConfigs[0] instanceof TableConfig){
            this.generateConfigs = (TableConfig[]) generateConfigs;
        }else if(generateConfigs.length > 0 && generateConfigs[0] instanceof List<?>){
            this.generateConfigs = new TableConfig[generateConfigs.length];
            for (int i = 0; i < generateConfigs.length; i++) {
                TableConfig tableConfig = new TableConfig();
                List<Object> c = (List<Object>) generateConfigs[i];
                Iterator<Object> iterator = c.iterator();
                if(iterator.hasNext()) tableConfig.setTableName((String) iterator.next());
                if(iterator.hasNext()) tableConfig.setTableComment((String) iterator.next());
                if(iterator.hasNext()) tableConfig.setGenerateVue3((String) iterator.next());
                if(iterator.hasNext()) tableConfig.setPrimaryKey((String) iterator.next());
                if(iterator.hasNext()) tableConfig.setSequence((String) iterator.next());
                if(iterator.hasNext()) tableConfig.setRequestMapping((String) iterator.next());
                this.generateConfigs[i] = tableConfig;
            }
        }
    }

    public void setRootImplPath(String rootImplPath) {
        this.rootImplPath = rootImplPath;
        this.rootControllerPath = rootImplPath;
    }



    public static Config getConfig(String model) {
        if(c != null){
            return c;
        }
        String dir = System.getProperty("user.dir");
        String name = "config";
        if(model != null && model.length() > 0){
            name = name + "-" +model;
        }
        // 本地运行
        String configPath = dir + "/generate-code/" + name + ".json";
        File file = new File(configPath);
        if (!file.exists()) {
            // 插件形式
            configPath = dir + "/config.json";
            file = new File(configPath);
            if(!file.exists()){
                StringBuilder2 tempString = FileUtils.getTempString("config.json");
                FileUtils.write(file, tempString.toString());
                throw new RuntimeException("配置文件不存在，已自动生成，在插件所在包根目的config.json文件，请自行修改");
            }
        }
        String reade = FileUtils.reade(file);
        if (reade == null) {
            throw new RuntimeException("配置文件读取失败，请检查文件格式");
        }
        try {
            // 去除 以 "// " 开头的行
            reade = reade.replaceAll("// .*", "")
                    .replaceAll("\\s", "")
                    .replace(",],", "],");
            c = JsonUtil.parseObject(reade, Config.class);
            c.model = model;
            return c;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
